//
//  FansAndAttentionController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/14.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "FansAndAttentionController.h"
#import "FansAndAttentionCell.h"
#import "FansAndAttentionModel.h"

@interface FansAndAttentionController ()
/**粉丝按钮*/
@property (nonatomic,strong)UIButton*fansBtn;
/**关注按钮*/
@property (nonatomic,strong)UIButton*attentionBtn;

/**粉丝底部线条*/
@property (nonatomic,strong)UIView*fanLine;
/**关注底部线条*/
@property (nonatomic,strong)UIView*attenLine;

/**listViewTool*/
@property (nonatomic,strong)UITableViewTool*listViewTool;
/**数据源*/
@property (nonatomic,strong)NSMutableArray*dataSoure;
/**<#属性#>*/
@property (nonatomic,strong)NSMutableArray*dataArray;
@end

@implementation FansAndAttentionController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
}
-(void)createUI{
    self.navigationItem.titleView = [self titleView];
    self.navigationItem.titleView.userInteractionEnabled = YES;

    [self.listViewTool.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}
#pragma mark --懒加载
-(MyTitleView *)titleView{
    MyTitleView * view = [[MyTitleView alloc] initWithFrame:CGRectMake(90, 0, KScreenWidth-180, K_TopHeight)];
    view.userInteractionEnabled = YES;
    view.intrinsicContentSize = CGSizeMake(KScreenWidth-180,K_TopHeight);
    
    self.fanLine = [UIView new];
    self.fanLine.backgroundColor = UIColorFromRGB(0x009966);
    
    self.attenLine = [UIView new];
    self.attenLine.backgroundColor = UIColorFromRGB(0x009966);
    
    [view addSubview:self.fansBtn];
    [view addSubview:self.attentionBtn];
    [view addSubview:self.fanLine];
    [view addSubview:self.attenLine];
    
    self.fanLine.hidden = !self.fansBtn.selected;
    self.attenLine.hidden = !self.attentionBtn.selected;
    
    [self.fansBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.centerY.mas_equalTo(0);
    }];
    [self.fanLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.fansBtn);
        make.height.mas_equalTo(3);
        make.width.mas_equalTo(20);
        make.top.mas_equalTo(self.fansBtn.mas_bottom).offset(1);
    }];
    
    [self.attentionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(self.fansBtn.mas_right).offset(116);
    }];
    
    [self.attenLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.attentionBtn);
        make.height.mas_equalTo(3);
        make.width.mas_equalTo(20);
        make.top.mas_equalTo(self.attentionBtn.mas_bottom).offset(1);
    }];
    
    
    return view;
}
-(UIButton*)fansBtn{
    if(!_fansBtn){
        _fansBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [_fansBtn setTitle:@"粉丝" forState:UIControlStateNormal];
        [_fansBtn setTitleColor:UIColorFromRGB(0x009966) forState:UIControlStateSelected];
        [_fansBtn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
        _fansBtn.selected = self.type == FansType ? YES : NO;
        kWeakSelf(weakSelf);
        [_fansBtn addActionHandler:^{
            weakSelf.fansBtn.selected = YES;
            weakSelf.fanLine.hidden = NO;

            weakSelf.attentionBtn.selected = NO;
            weakSelf.attenLine.hidden = YES;
            
            [weakSelf.listViewTool reloadData:weakSelf.dataSoure];

        }];
    }
    return _fansBtn;
}
-(UIButton*)attentionBtn{
    if(!_attentionBtn){
        _attentionBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [_attentionBtn setTitle:@"关注" forState:UIControlStateNormal];
        [_attentionBtn setTitleColor:UIColorFromRGB(0x009966) forState:UIControlStateSelected];
        [_attentionBtn setTitleColor:UIColorFromRGB(0x333333) forState:UIControlStateNormal];
        _attentionBtn.selected = self.type == AttentionType ? YES : NO;
        kWeakSelf(weakSelf);
        [_attentionBtn addActionHandler:^{
            weakSelf.fansBtn.selected = NO;
            weakSelf.fanLine.hidden = YES;
            
            weakSelf.attentionBtn.selected = YES;
            weakSelf.attenLine.hidden = NO;
            
            [weakSelf.listViewTool reloadData:weakSelf.dataArray];
        }];

    }
    return _attentionBtn;
}
-(UITableViewTool*)listViewTool{
    if(!_listViewTool){
        _listViewTool=[[UITableViewTool alloc] initWithFrame:CGRectZero style:UITableViewStylePlain registerCellClassName:NSStringFromClass([FansAndAttentionCell class]) identifier:NSStringFromClass([FansAndAttentionCell class]) dataArray:self.type == FansType ? self.dataSoure : self.dataArray delegate:self dataSourceBlock:^(id  _Nonnull TempCell, NSIndexPath * _Nonnull indexPath, id  _Nonnull TempData) {
            FansAndAttentionCell * cell = (FansAndAttentionCell*)TempCell;
            FansAndAttentionModel * model = (FansAndAttentionModel *)TempData;
            
            cell.model = model;
            
        } selectCell:^(id  _Nonnull TempCell, NSIndexPath * _Nonnull indexPath, id  _Nonnull TempData) {
            
        }];
        [_listViewTool.listView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        [_listViewTool.listView setSeparatorColor:UIColorFromRGB(0xE7EEE7)];
    }
    return _listViewTool;
}
-(NSMutableArray *)dataSoure{
    if (!_dataSoure) {
        _dataSoure =[NSMutableArray array];
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Fans" ofType:@"plist"];
           //当数据结构为数组时
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
        [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            FansAndAttentionModel *model = [FansAndAttentionModel new];
            [model setValuesForKeysWithDictionary:obj];
            [_dataSoure addObject:model];
        }];
    }
    return _dataSoure;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray =[NSMutableArray array];
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Attention" ofType:@"plist"];
           //当数据结构为数组时
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
        [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            FansAndAttentionModel *model = [FansAndAttentionModel new];
            [model setValuesForKeysWithDictionary:obj];
            [_dataArray addObject:model];
        }];

    }
    return _dataArray;
}
@end

@interface MyTitleView ()
@end

@implementation MyTitleView

@end
